﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CIT.WMS.Other
{
    public partial class FrmPicking2 : Form
    {
        string _StorageSN;
        string _FromLocation;
        string _PartNumber;
        string _Version;
        decimal _Qty;
        string _ToLocation;
        string _BillNo;

        public FrmPicking2(string billNo, string locationSN, string partNumber)
        {
            InitializeComponent();
            _FromLocation = locationSN;
            _PartNumber = partNumber;
            _BillNo = billNo;
        }

        public static bool CheckLocationSN(string locationSN)
        {
            try
            {
                string strSql = @"select Storage_Type from  wms_T_Inventory a inner join T_Bllb_Storage_tbs b
on a.Storage_SN = b.Storage_SN where a.Location_SN = '{0}'";
                strSql = string.Format(strSql, locationSN);
                var dt = Utils.NMS.GetDataTable(strSql);
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0][0].ToString() == "201")
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private void FrmTranseferIn_Load(object sender, EventArgs e)
        {
            FrmReset();
            ShowInfo();
        }

        private void ShowInfo()
        {
            string sql = string.Format(@"SELECT * FROM dbo.wms_T_Inventory WHERE Location_SN='{0}' AND PartNumber='{1}'", _FromLocation, _PartNumber);
            var dtIvt = Utils.NMS.GetDataTable(sql);

            if (dtIvt.Rows.Count == 0)
            {
                areaMsg.Text = "当前物料库存已不存在，请确认！";
            }
            _StorageSN = dtIvt.Rows[0]["Storage_SN"].ToString();
            _PartNumber = dtIvt.Rows[0]["PartNumber"].ToString();
            _Version = dtIvt.Rows[0]["Version"].ToString();

            areaInfo.Text = "当前仓库：" + _StorageSN +
                    "\r\n当前物料：" + dtIvt.Rows[0]["PartNumber"].ToString() +
                    "\r\n库存数量：" + dtIvt.Rows[0]["Qty"].ToString();
            _Qty = Convert.ToDecimal(dtIvt.Rows[0]["Qty"].ToString());
            this.txt_FromLocation.Text = _FromLocation;
            this.txt_PartNumber.Text = _PartNumber;

            string sqlPick = "SELECT * FROM T_Bllb_StorageLocation_tbsl WHERE Location_SN LIKE '{0}%' AND Area_Standard='Picking'";
            sqlPick = string.Format(sqlPick, _StorageSN);
            var dtPick = Utils.NMS.GetDataTable(sqlPick);
            if (dtPick.Rows.Count == 0)
            {
                areaMsg.Text = "当前仓库没有拣货库位，请确认！";
            }
            cmb_ToLocation.ValueMember = "Location_SN";
            cmb_ToLocation.DisplayMember = "Location_SN";
            cmb_ToLocation.DataSource = dtPick;
        }

        /// <summary>
        /// 数量输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Qty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            decimal qty = 0;
            try
            {
                qty = Convert.ToDecimal(txt_Qty.Text);
            }
            catch
            {
                areaMsg.Text = "请输入正确的数字！";
                txt_Qty.Text = "";
                return;
            }

            if (qty == 0)
            {
                areaMsg.Text = "数量不能为0！";
                txt_Qty.Text = "";
                return;
            }
            if (qty > _Qty)
            {
                areaMsg.Text = "数量不能大于库存数量！";
                txt_Qty.Text = "";
                return;
            }

            string storageSN;
            string aresSN;
            try
            {
                _ToLocation = cmb_ToLocation.SelectedValue.ToString();
                var snArray = _ToLocation.Split('#');

                storageSN = snArray[0];
                aresSN = snArray[0] + '#' + snArray[1];

                if (!string.IsNullOrEmpty(_StorageSN) && !_StorageSN.Equals(storageSN))
                {
                    areaMsg.Text = "上架仓库" + _StorageSN + "与扫描的仓库" + storageSN + "不一致！";
                    return;
                }
            }
            catch
            {
                areaMsg.Text = "库位" + _ToLocation + "格式不正确，请重新输入!";
                return;
            }

            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("是否把物料转移至拣货库位？", "上架确认", messButton, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);

            if (dr == DialogResult.OK)
            {
                var ret = Query(_BillNo, _PartNumber, Utils.uContext.UserNamek__BackingField, _FromLocation, aresSN, _ToLocation, "", qty);

                if (ret.Equals("OK"))
                {
                    areaMsg.Text = txt_PartNumber.Text + "上架成功!";
                    this.txt_Qty.Enabled = false;
                    this.Close();
                }
                else
                {
                    areaMsg.Text = txt_PartNumber.Text + "上架失败!\r\n" + ret;

                }
            }
        }

        #region 内部方法
        private void FrmReset()
        {
            txt_FromLocation.Enabled = false;
            txt_PartNumber.Enabled = false;
            txt_Qty.Enabled = true;

            areaMsg.Text = "";
            areaInfo.Text = "";

            txt_FromLocation.Text = "";
            txt_Qty.Text = "";

            _StorageSN = "";
            //_FromLocation = "";
            //_PartNumber = "";
            _Version = "";
            _Qty = 0;
            _ToLocation = "";

        }

        /// <summary>
        /// 判断时间格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private bool IsDateTime(string str)
        {
            System.IFormatProvider format = new System.Globalization.CultureInfo("zh-CN", true);
            try
            {
                DateTime.ParseExact(str, "yyyyMMdd", format);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private string Query(string inBillNo, string inPartNumber, string inUserName,
                                string inOLocationSN, string inIArea, string inILocationSN, string inDateCode, decimal inQty)
        {
            string sqlcmd = string.Format(@"begin 
                                            DECLARE @ret varchar(50);
                                            exec PDA_MoveForPick_query N'{0}',N'{1}',N'{2}',N'{3}',N'{4}',N'{5}',N'{6}',{7},@ret output;
                                            SELECT @ret 
                                            end", inBillNo, inPartNumber, inUserName, inOLocationSN, inIArea, inILocationSN, inDateCode, inQty);
            var result = Utils.NMS.GetDataTable(sqlcmd);
            return result.Rows[0][0].ToString();
        }
        #endregion

        private void cmb_ToLocation_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
            {
                e.Handled = true;
                return;
            }

            txt_Qty.Focus();
            return;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //END
    }
}